package cn.db101.gocentdaily.config;

import cn.db101.gocentdaily.model.ResultModel;
import cn.dev33.satoken.exception.NotLoginException;
import cn.hutool.http.HttpStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Created by zooy on 17-6-20.
 */
@RestControllerAdvice
@Slf4j
public class ExceptionControllerAdvice {

    @ExceptionHandler(Exception.class)
    public ResultModel exception(Throwable ex) {

        log.error("system error", ex);
        return ResultModel.failed("系统错误");
    }

    @ExceptionHandler(value = NotLoginException.class)
    public ResultModel notLoginException(NotLoginException notLogin) {

        String message = "";
        // 判断场景值，定制化异常信息
        if (notLogin.getType().equals(NotLoginException.NOT_TOKEN)) {
            message = "未提供token";
        } else if (notLogin.getType().equals(NotLoginException.INVALID_TOKEN)) {
            message = "token无效";
        } else if (notLogin.getType().equals(NotLoginException.TOKEN_TIMEOUT)) {
            message = "token已过期";
        } else if (notLogin.getType().equals(NotLoginException.BE_REPLACED)) {
            message = "token已被顶下线";
        } else if (notLogin.getType().equals(NotLoginException.KICK_OUT)) {
            message = "token已被踢下线";
        } else {
            message = "当前会话未登录";
        }

        return ResultModel.failed(HttpStatus.HTTP_BAD_REQUEST, message);
    }

}
